<template>
  <div>
    <div style="margin-bottom:5px">
      <el-input
          v-model="param.goodsname"
          placeholder="请输入物品名"
          style="width: 200px;"
          prefix-icon="el-icon-search"
          @keyup.enter.native="loadPost">
      </el-input>
      <el-select v-model="param.storagename" placeholder="请选择仓库" style="width: 200px;" @change="loadPost">
        <el-option v-for="item in storageData" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select>

      <el-select v-model="param.goodstypename" placeholder="请选择分类" style="width: 200px;" @change="loadPost">
        <el-option v-for="item in goodsTypeData" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select>

      <el-button size="medium" type="primary"  style="margin-left: 5px" @click="loadPost">查询</el-button>
      <el-button size="medium" type="success" @click="CleanName" style="margin-left: 5px">重置</el-button>
      <el-button size="medium" type="primary" @click="add" style="margin-left: 5px">新增</el-button>
    </div>

    <el-table :data="tableData"
              :header-cell-style="{background: 'rgba(103,146,146,0.38)'}"
              border>
      <el-table-column prop="id" label="ID" width="140"></el-table-column>
      <el-table-column prop="goodsname" label="物品名" width="120" ></el-table-column>
      <el-table-column prop="storagename" label="物品仓库名" width="120"></el-table-column>
      <el-table-column prop="goodstypename" label="物品分类名" width="120"></el-table-column>
      <el-table-column prop="username" label="用户" width="120"></el-table-column>
      <el-table-column prop="adminname" label="管理" width="120"></el-table-column>
      <el-table-column prop="count" label="数量" width="120"></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column prop="operate" label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm
              title="这是一段内容确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button slot="reference" size="small" type="danger">删除</el-button>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%">
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="商品" prop="goodsId">
          <el-select v-model="form.goodsId" placeholder="商品">
            <el-option
                v-for="item in goodsData"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户" prop="goodsId">
          <el-select v-model="form.userId" placeholder="用户">
            <el-option
                v-for="item in userData"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管理" prop="adminId">
          <el-select v-model="form.adminId" placeholder="管理">
            <el-option
                v-for="item in adminData"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="count">
          <el-input v-model="form.count"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { loadPost, loadGoods, loadGoodsType, loadStorage, loadUser } from "@/api";
export default {
  name: "RecordManage",
  data() {
    return {
      param:{
        goodsname:'',
        storagename:'',
        goodstypename:'',
    },
      tableData: [],
      storageData: [],
      goodsTypeData: [],
      goodsData: [],
      perData: [],
      userData: [],
      adminData: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      ID: "",
      dialogVisible: false,
      form: {
        id: '',
        goodsId: '',
        userId: '',
        adminId: '',
        count: '',
        remark: '',
      },
      rules: {
        // name: [{ required: true, message: "请输入记录", trigger: "blur" }],
      }
    };
  },
  methods: {
    resetForm() {
      this.$refs.form.resetFields();
    },
    async doSave() {
      try {
        const res = await loadPost(this.form);
        if (res.data.code === 200) {
          this.$message.success("操作成功！");
          this.dialogVisible = false;
          this.loadPost();
        } else {
          this.$message.error("操作失败！");
        }
      } catch (error) {
        this.$message.error("请求出错：" + error.message);
      }
    },
    async doMod() {
      try {
        const res = await loadPost(this.form);
        if (res.data.code === 200) {
          this.$message.success("操作成功！");
          this.dialogVisible = false;
          this.loadPost();
        } else {
          this.$message.error("操作失败！");
        }
      } catch (error) {
        this.$message.error("请求出错：" + error.message);
      }
    },
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id) {
            this.doMod();
          } else {
            this.doSave();
          }
        } else {
          this.$message.error("请检查输入信息！");
        }
      });
    },
    mod(row) {
      this.form = { ...row }; // 直接复制对象，避免修改原数据
      this.dialogVisible = true;
    },
    async del(id) {
      try {
        const res = await this.$axios.get(`/record/del?id=${id}`);
        if (res.data.code === 200) {
          this.$message.success("删除成功！");
          this.loadPost();
        } else {
          this.$message.error("删除失败！");
        }
      } catch (error) {
        this.$message.error("请求失败：" + error.message);
      }
    },
    add() {
      this.form = {};
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.resetForm();
      });
    },
    CleanName() {
      this.name = "";
      this.loadPost();
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.loadPost();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.loadPost();
    },
    async fetchData(apiMethod, successCallback) {
      try {
        const res = await apiMethod();
        if (res.status === 200 || res.data.code === 200) {
          successCallback(res.data);
        } else {
          this.$message.error("获取数据失败");
        }
      } catch (error) {
        this.$message.error("请求失败：" + error.message);
      }
    },
    loadPost() {
      this.fetchData(() => loadPost({
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        ...this.param
      }), (data) => {
        this.tableData = data.data;
        this.total = data.total;
      });
    },
    loadGoods() {
      this.fetchData(loadGoods, (data) => { this.goodsData = data; });
    },
    loadGoodsType() {
      this.fetchData(loadGoodsType, (data) => { this.goodsTypeData = data; });
    },
    loadStorage() {
      this.fetchData(loadStorage, (data) => { this.storageData = data; });
    },
    loadUser() {
      this.fetchData(loadUser, (data) => {
        this.perData = data;
        this.userData = data.filter(user => user.roleId === 2);
        this.adminData = data.filter(user => user.roleId === 1);
      });
    },
  },
  beforeMount() {
    this.loadPost();
    this.loadGoods();
    this.loadUser();
    this.loadGoodsType();
    this.loadStorage();
  },
};
</script>



<style scoped>
</style>
